package com.htandroid.htandroid.aty;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.ab.soap.AbSoapUtil;
import com.ab.util.AbToastUtil;
import com.ab.view.ioc.AbIocView;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.entity.Costobj;
import com.htandroid.htandroid.sys.InstorageURL;
import com.htandroid.htandroid.sys.base_index;
import com.htandroid.htandroid.webservice.BuildQgdListXMLService;
import com.htandroid.htandroid.webservice.XMLCostobjlist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.htandroid.htandroid.constance.ConstanceServer.BaseUrl_New;

/**
 * Created by lkp on 2015/11/29.
 */
//存货档案选择
public class Basenvo_Choosecostobj extends base_index {
    private ArrayList<Costobj> cobjlist;
    private String btntype;
    private String ccode;

    @AbIocView(id = R.id.btn1_tv, click = "btnClick")
    TextView btn1_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.basenvo_rchoose);
        initview("部门档案");
        initview2();
    }

    public void btnClick(View v) {
        Intent intent = new Intent();
        //从position改为id,因为过滤之后的position不正确
        intent.putExtra("ccode", "");
        intent.putExtra("btntype", btntype);
        intent.putExtra("cname", "");
        setResult(R.layout.basenvo_rchoose / 200000, intent);  //设置返回结果
        finish();
    }

    //初始化2
    @Override
    public void initview2() {
        //获取传递的信息
        Intent intent = getIntent();// 创建Intent对象
        Bundle bundle = intent.getExtras();// 获取传入的数据，并使用Bundle记录
        btntype = bundle.getString("btntype");
        ccode = bundle.getString("ccode");
        cobjlist = myApplication.getGv_costobj();
        if (cobjlist == null || cobjlist.size() < 1) {
            //获取供应商列表
            getdata(0);
        } else {
            createrl();
        }

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Costobj cobj = cobjlist.get((int) id);
                Intent intent = new Intent();
                //从position改为id,因为过滤之后的position不正确
                intent.putExtra("ccode", cobj.getCcode());
                intent.putExtra("btntype", btntype);
                intent.putExtra("cname", cobj.getCname());
                setResult(R.layout.basenvo_rchoose / 200000, intent);  //设置返回结果
                finish();
            }
        });
    }

    //获取数据
    @Override
    public void getdata(int page) {
        //参数
        String urlString = "http://" + InstorageURL.SERVERURL  + BaseUrl_New;
        String[] parms = {"cacc_id", "iyear"};
        String[] vals = {myApplication.getGs_ztid(), myApplication.getGs_year()};
        //生成xml
        BuildQgdListXMLService service = new BuildQgdListXMLService();
        String xmlstring = service.buildXML(null, "costobjlist", InstorageURL.NAME_SPACE, parms, vals, null);

        mAbSoapUtil.xmlCall(urlString, InstorageURL.NAME_SPACE, "costobjlist", xmlstring, new AbSoapUtil.WebServiceCallBack() {
            @Override
            public void onSucced(String result) {
                //解析xml文件,返回列表
                if (result != null) {
                    cobjlist = XMLCostobjlist.paraseCommentInfors(result);
                    myApplication.setGv_costobj(cobjlist);
                    createrl();
                } else {
                    AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
                }
            }

            @Override
            public void onFailure(String result) {
                AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
                //swipeRefreshLayout.setRefreshing(false);
                //ToolLog.log("err：" + result);
            }
        });
    }

    public void createrl() {
        adatperDataList.clear();
        ArrayList<Map<String, Object>> dataListForInit = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < cobjlist.size(); i++) {
            Costobj cobj = new Costobj();
            cobj = cobjlist.get(i);
            //增加一个行选择状态初始化
            if (ccode != null && cobj.getCcode().equals(ccode)) {
                sa.getIsSelected().put(i, true);
            } else {
                sa.getIsSelected().put(i, false);
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("index", (i + 1));
            map.put("cvouchid", "编号：" + cobj.getCcode());
            map.put("cdept", "名称：" + cobj.getCname());
            map.put("cnocheckreason", "");
            map.put("ddate", "");
            map.put("more", "0");
            dataListForInit.add(map);
        }
        toolDataProvider.initData(dataListForInit);
        loadList();
        //mHandler.sendEmptyMessage(LIST_GET_FIRST_PAGE); //加载完毕发送指令显示第一页
        //swipeRefreshLayout.setRefreshing(false);
    }
}
